package com.mystnihon.freeremote.db.table;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import com.mystnihon.freeremote.db.DBProvider;

public class SeedboxTable extends GenericTable implements Parcelable {
	private static final String TAG = SeedboxTable.class.getSimpleName();

	public static final String TABLE_NAME = "seedbox";
	public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.freeremote." + TABLE_NAME;

	/**
	 * The MIME type of a {@link #CONTENT_URI} sub-directory of a single note.
	 */
	public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.freeremote." + TABLE_NAME;

	public static final Uri CONTENT_URI = Uri.parse("content://" + DBProvider.AUTHORITY + "/" + TABLE_NAME);
	public static final String COLUMN_NAME = "name";
	public static final String COLUMN_HOST = "host";

	public static final String COLUMN_SEED_LOGIN = "seedbox_login";
	public static final String COLUMN_SEED_PASSWORD = "seedbox_password";
	public static final String COLUMN_SEED_PORT = "seedbox_port";

	public static final String COLUMN_FTP_LOGIN = "seedbox_ftp_login";
	public static final String COLUMN_FTP_PASSWORD = "seedbox_ftp_password";
	public static final String COLUMN_FTP_PORT = "seedbox_ftp_port";

	public int mId;
	public String mName;
	public String mHost;

	public String seedboxLogin;
	public String seedboxPassword;
	public int seedboxPort;

	public String ftpLogin;
	public String ftpPassword;
	public int ftpPort;

	// column position in projection
	public static final int CONTENT_NAME_COLUMN = 1;
	public static final int CONTENT_HOST_COLUMN = 2;
	public static final int CONTENT_SEEDBOX_LOGIN_COLUMN = 3;
	public static final int CONTENT_SEEDBOX_PASSWORD_COLUMN = 4;
	public static final int CONTENT_SEEDBOX_PORT_COLUMN = 5;
	public static final int CONTENT_SEEDBOX_FTP_LOGIN_COLUMN = 6;
	public static final int CONTENT_SEEDBOX_FTP_PASSWORD_COLUMN = 7;
	public static final int CONTENT_SEEDBOX_FTP_PORT_COLUMN = 8;

	public static String[] CONTENT_PROJECTION = new String[] { _ID, COLUMN_NAME, COLUMN_HOST, COLUMN_SEED_LOGIN,
			COLUMN_SEED_PASSWORD, COLUMN_SEED_PORT, COLUMN_FTP_LOGIN, COLUMN_FTP_PASSWORD, COLUMN_FTP_PORT };

	public static String getCreateStatement() {
		final StringBuffer str = new StringBuffer("CREATE TABLE ");
		str.append(TABLE_NAME);
		str.append(" (");
		str.append(_ID);
		str.append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
		str.append(COLUMN_NAME);
		str.append(" TEXT, ");
		str.append(COLUMN_HOST);
		str.append(" TEXT, ");
		str.append(COLUMN_SEED_LOGIN);
		str.append(" TEXT, ");
		str.append(COLUMN_SEED_PASSWORD);
		str.append(" TEXT, ");
		str.append(COLUMN_SEED_PORT);
		str.append(" INTEGER, ");
		str.append(COLUMN_FTP_LOGIN);
		str.append(" TEXT, ");
		str.append(COLUMN_FTP_PASSWORD);
		str.append(" TEXT, ");
		str.append(COLUMN_FTP_PORT);
		str.append(" INTEGER); ");

		Log.d(TAG, "create court table: " + str.toString());
		return str.toString();
	}

	public ContentValues getContentValues() {
		final ContentValues content = new ContentValues();
		content.put(COLUMN_NAME, mName);
		content.put(COLUMN_HOST, mHost);
		content.put(COLUMN_SEED_LOGIN, seedboxLogin);
		content.put(COLUMN_SEED_PASSWORD, seedboxPassword);
		content.put(COLUMN_SEED_PORT, seedboxPort);
		content.put(COLUMN_FTP_LOGIN, ftpLogin);
		content.put(COLUMN_FTP_PASSWORD, ftpPassword);
		content.put(COLUMN_FTP_PORT, ftpPort);
		return content;
	}

	public static SeedboxTable inflateObject(Cursor c) {
		final SeedboxTable seedbox = new SeedboxTable();
		seedbox.mId = (c.getInt(CONTENT_ID_COLUMN));
		seedbox.mName = c.getString(CONTENT_NAME_COLUMN);
		seedbox.mHost = c.getString(CONTENT_HOST_COLUMN);
		seedbox.seedboxLogin = c.getString(CONTENT_SEEDBOX_LOGIN_COLUMN);
		seedbox.seedboxPassword = c.getString(CONTENT_SEEDBOX_PASSWORD_COLUMN);
		seedbox.seedboxPort = c.getInt(CONTENT_SEEDBOX_PORT_COLUMN);
		seedbox.ftpLogin = c.getString(CONTENT_SEEDBOX_FTP_LOGIN_COLUMN);
		seedbox.ftpPassword = c.getString(CONTENT_SEEDBOX_FTP_PASSWORD_COLUMN);
		seedbox.ftpPort = c.getInt(CONTENT_SEEDBOX_FTP_PORT_COLUMN);
		return seedbox;
	}

	public SeedboxTable() {

	}

	public SeedboxTable(Parcel in) {
		mId = in.readInt();
		mName = in.readString();
		mHost = in.readString();
		seedboxLogin = in.readString();
		seedboxPassword = in.readString();
		seedboxPort = in.readInt();
		ftpLogin = in.readString();
		ftpPassword = in.readString();
		ftpPort = in.readInt();

	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeInt(mId);
		dest.writeString(mName);
		dest.writeString(mHost);
		dest.writeString(seedboxLogin);
		dest.writeString(seedboxPassword);
		dest.writeInt(seedboxPort);
		dest.writeString(ftpLogin);
		dest.writeString(ftpPassword);
		dest.writeInt(ftpPort);
	}

	public static final Parcelable.Creator<SeedboxTable> CREATOR = new Creator<SeedboxTable>() {

		@Override
		public SeedboxTable[] newArray(int size) {
			return new SeedboxTable[size];
		}

		@Override
		public SeedboxTable createFromParcel(Parcel source) {
			return new SeedboxTable(source);
		}
	};
}
